package com.ray.job;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import com.ray.bean.AirFlowControlBean;
import com.ray.bean.TemporalIntervalBean;
import com.ray.mapper.AirFlowControlMapper;
import com.ray.mapper.AirFlowControlRelationTimeIntervalMapper;
import com.ray.mapper.TemporalIntervalMapper;

@Service
public class AirFlowContralJob {

	@Autowired
	private AirFlowControlMapper airFlowControlMapper;
	@Autowired
	private TemporalIntervalMapper temporalIntervalMapper;
	@Autowired
	private AirFlowControlRelationTimeIntervalMapper airFlowControlRelationTimeIntervalMapper;

	@Scheduled(cron = "30 1-59 * * * ?")
	// 每分钟执行一次
	public void updataSwitchByTimeAndHandOperation() {
		List<AirFlowControlBean> airFlowControlBeans = airFlowControlMapper
				.getAllAirFlowControlBeans();
		boolean shouldSetON = false;
		for (AirFlowControlBean bean : airFlowControlBeans) {
			if (!bean.isHandOperating()) {
				for (TemporalIntervalBean temporalIntervalBean : bean
						.getTemporalIntervalBeans()) {
					if (temporalIntervalBean.checkNowInTemporalInterval()) {
						shouldSetON = true;
					}
				}
				if (shouldSetON) {
					if (!bean.airFlowControlIsOpen()) {
						bean.toggleAirFlowControlSwitch();
						airFlowControlMapper.updataAirFlowControl(bean);
					}
				} else {
					if (bean.airFlowControlIsOpen()) {
						bean.toggleAirFlowControlSwitch();
						airFlowControlMapper.updataAirFlowControl(bean);
					}
				}
			}

		}
	}
}
